import numpy as np
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
from sklearn.model_selection import cross_val_score
from scipy.stats import ttest_rel

# 加载数据
iris = load_iris()
X, y = iris.data, iris.target

# 初始化模型
model_a = RandomForestClassifier(random_state=42)
model_b = GradientBoostingClassifier(random_state=42)

# 5折交叉验证测准确率
scores_a = cross_val_score(model_a, X, y, cv=5, scoring='accuracy')  # 比如输出：[0.9667, 0.9667, 0.9333, 0.9667, 1.0]
scores_b = cross_val_score(model_b, X, y, cv=5, scoring='accuracy')  # 比如输出：[0.9667, 0.9333, 0.9333, 0.9667, 0.9667]

# 配对t检验：比较两模型的每折差异
t_stat, p_value = ttest_rel(scores_a, scores_b)

print(f"模型A平均准确率: {np.mean(scores_a):.4f} ± {np.std(scores_a):.4f}")
print(f"模型B平均准确率: {np.mean(scores_b):.4f} ± {np.std(scores_b):.4f}")
print(f"t统计量: {t_stat:.3f}, p值: {p_value:.5f}")

# 结论
if p_value < 0.05:
    print("模型性能差异显著！")
else:
    print("差异不显著，可能是随机波动～")